library(plotly)x <-c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013)y_television <-c(74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69)y_internet <-c(13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50)data <-data.frame(x, y_television, y_internet)xaxis <-list(title ="Año",showline =TRUE,showgrid =TRUE,gridcolor ='rgb(230,230,250)', # lila clarolinecolor ='rgb(160, 82, 170)',linewidth =2,tickcolor ='rgb(160, 82, 170)',ticks ='outside',tickfont =list(family ='Arial', size =12, color ='#3c003f'))yaxis <-list(title ="Porcentaje",showline =TRUE,showgrid =TRUE,gridcolor ='rgb(230,230,250)', # lila clarolinecolor ='rgb(160, 82, 170)',tickcolor ='rgb(160, 82, 170)',ticks ='outside',tickfont =list(family ='Arial', size =12, color ='#3c003f'))margin <-list(l =80, r =80, t =80, b =80)# Anotacionestelevision_1 <-list(xref ='paper', yref ='y', x =0.05, y = y_television[1],text =paste('Televisión', y_television[1], '%'),font =list(family ='Arial', size =14, color ='#6a0dad'), showarrow =FALSE)internet_1 <-list(xref ='paper', yref ='y', x =0.05, y = y_internet[1],text =paste('Internet', y_internet[1], '%'),font =list(family ='Arial', size =14, color ='#9d4edd'), showarrow =FALSE)television_2 <-list(xref ='paper', x =0.95, y = y_television[12],text =paste('Televisión', y_television[12], '%'),font =list(family ='Arial', size =14, color ='#6a0dad'), showarrow =FALSE)internet_2 <-list(xref ='paper', x =0.95, y = y_internet[12],text =paste('Internet', y_internet[12], '%'),font =list(family ='Arial', size =14, color ='#9d4edd'), showarrow =FALSE)# Gráficofig <-plot_ly(data, x =~x) %>%add_trace(y =~y_television, type ='scatter', mode ='lines',line =list(color ='#6a0dad', width =2), name ='Televisión') %>%add_trace(y =~y_internet, type ='scatter', mode ='lines',line =list(color ='#9d4edd', width =2), name ='Internet') %>%add_trace(x =~c(x[1], x[12]), y =~c(y_television[1], y_television[12]),type ='scatter', mode ='markers',marker =list(color ='#6a0dad', size =6)) %>%add_trace(x =~c(x[1], x[12]), y =~c(y_internet[1], y_internet[12]),type ='scatter', mode ='markers',marker =list(color ='#9d4edd', size =6)) %>%layout(title =list(text ="Fuente principal de noticias", font =list(size =20, color ='#3c003f')),xaxis = xaxis,yaxis = yaxis,margin = margin,annotations =list(television_1, internet_1, television_2, internet_2),showlegend =FALSE,plot_bgcolor ='rgb(248,244,252)',paper_bgcolor ='white')fig
Relleno entre Líneas
Code
library(plotly)month <-c('January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December')high_2014 <-c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)low_2014 <-c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)data <-data.frame(month, high_2014, low_2014)data$average_2014 <-rowMeans(data[, c("high_2014", "low_2014")])data$month <-factor(data$month, levels = data[["month"]])fig <-plot_ly(data, x =~month, y =~high_2014, type ='scatter', mode ='lines',line =list(color ='transparent'),showlegend =FALSE, name ='High 2014')fig <- fig %>%add_trace(y =~low_2014, type ='scatter', mode ='lines',fill ='tonexty',fillcolor ='rgba(106,13,173,0.2)', # morado claro y transparenteline =list(color ='transparent'),showlegend =FALSE, name ='Low 2014')fig <- fig %>%add_trace(x =~month, y =~average_2014, type ='scatter', mode ='lines',line =list(color ='rgb(106,13,173)', width =3), # morado oscuroname ='Average')fig <- fig %>%layout(title ="Average, High and Low Temperatures in New York",paper_bgcolor ='rgb(255,255,255)',plot_bgcolor ='rgb(242,235,247)', # fondo muy claro moradoxaxis =list(title ="Months",gridcolor ='rgb(255,255,255)',showgrid =TRUE,showline =FALSE,showticklabels =TRUE,tickcolor ='rgb(127,127,127)',ticks ='outside',zeroline =FALSE),yaxis =list(title ="Temperature (degrees F)",gridcolor ='rgb(255,255,255)',showgrid =TRUE,showline =FALSE,showticklabels =TRUE,tickcolor ='rgb(127,127,127)',ticks ='outside',zeroline =FALSE))fig
Gráfica de Densidad
Code
dens <-with(diamonds, tapply(price, INDEX = cut, density))df <-data.frame(x =unlist(lapply(dens, "[[", "x")),y =unlist(lapply(dens, "[[", "y")),cut =rep(names(dens), each =length(dens[[1]]$x)))fig <-plot_ly(df, x =~x, y =~y, color =~cut) fig <- fig %>%add_lines()fig
Escalas de Colores Cualitativas
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, color =~Species)fig
Nombres de paletas ColorBrewer
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, color =~Species, colors ="Purples")fig
Mapeo de Datos de Símbolos
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, type ='scatter',mode ='markers', symbol =~Species, symbols =c('circle','x','o'),color =I('#6a0dad'), marker =list(size =10))fig
Agregar asignación de color y tamaño
Code
d <- diamonds[sample(nrow(diamonds), 1000), ]fig <-plot_ly( d, x =~carat, y =~price,color =~carat, size =~carat,colors =c("#ede1f5", "#6a0dad", "#2d0034") )fig